package com.yxdj.app.service.impl;

import com.yxdj.app.service.AppUserIndexService;
import com.yxdj.common.core.constant.SecurityConstants;
import com.yxdj.common.core.domain.R;
import com.yxdj.common.core.exception.ServiceException;
import com.yxdj.feedback.api.RemoteUserReviewService;
import com.yxdj.feedback.api.dto.UserReviewDto;
import com.yxdj.feedback.api.vo.UserReviewVo;
import com.yxdj.service.api.RemoteMainCategoryService;
import com.yxdj.service.api.RemoteServiceDetailService;
import com.yxdj.service.api.RemoteSubCategoryService;
import com.yxdj.service.api.domain.MainCategory;
import com.yxdj.service.api.domain.ServiceDetail;
import com.yxdj.service.api.domain.SubCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 韩总
 */
@Service
public class AppUserIndexServiceImpl implements AppUserIndexService {

    @Autowired
    private RemoteMainCategoryService remoteMainCategoryService;

    @Autowired
    private RemoteSubCategoryService remoteSubCategoryService;

    @Autowired
    private RemoteServiceDetailService remoteServiceDetailService;

    @Autowired
    private RemoteUserReviewService remoteUserReviewService;

    @Override
    public Map<String, Object> index() {
        //获取九个主分类
        R<List<MainCategory>> mainCategoryList = remoteMainCategoryService.getMainCategoryList(9, SecurityConstants.INNER);
        if(R.FAIL == mainCategoryList.getCode()){
            throw new ServiceException(mainCategoryList.getMsg());
        }
        //获取子服务分类
        R<List<SubCategory>> subCategoryList = remoteSubCategoryService.getSubCategoryList(3, SecurityConstants.INNER);
        if(R.FAIL == subCategoryList.getCode()){
            throw new ServiceException(subCategoryList.getMsg());
        }
        List<MainCategory> mainCategoryListData = mainCategoryList.getData();
        List<SubCategory> subCategoryListData = subCategoryList.getData();
        Map<String,Object> map = new HashMap<>();
        map.put("mainCategoryList",mainCategoryListData);
        map.put("subCategoryList",subCategoryListData);
        return map;
    }

    @Override
    public List<MainCategory> getAllCategory() {
        return remoteMainCategoryService.getAllCategory(SecurityConstants.INNER).getData();
    }

    @Override
    public ServiceDetail getServiceDetailById(Long id) {
        R<ServiceDetail> serviceDetailById = remoteServiceDetailService.getServiceDetailById(id, SecurityConstants.INNER);
        if(R.FAIL == serviceDetailById.getCode()){
            throw new ServiceException(serviceDetailById.getMsg());
        }
        return serviceDetailById.getData();
    }

    @Override
    public UserReviewVo getUserReview(UserReviewDto userViewDto) {
        R<UserReviewVo> userReviewList = remoteUserReviewService.getUserReviewList(userViewDto, SecurityConstants.INNER);
        if(R.FAIL == userReviewList.getCode()){
            throw new ServiceException(userReviewList.getMsg());
        }
        return userReviewList.getData();
    }

}
